package com.businesswebsite.businesswebsite.FormalWebExplot.Config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Hashtable;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @Author: 段美超
 * @CreateTime: 2025-05-20
 * @Description: shiro工程配置
 * @Version: 1.0
 */

@Configuration
public class ShiroConfig {
    /*注入顺序
    * ico->realm->securityManager->ShiroFilterFactoryBean
    * */
//    Qualifier注解从ioc容器获取realm对象
    @Bean
    public DefaultWebSecurityManager securityManager(@Qualifier("realm")Realm realm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(realm);
        return manager;
    }
    @Bean
    public Realm realm(){return new Realm();}
//    private static final Logger logger = LoggerFactory.getLogger(ShiroConfig.class);
//    配置文件读取开关
//    @Value("${jwt.auth}")
//    private boolean auth;

//    注入工厂
    @Bean
    public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
        filterFactoryBean.setSecurityManager(securityManager);
//        权限设置
        Map<String,String> map = new Hashtable<>();
//        设置对应路径权限
        map.put("/index","anon"); //放行_首页
        map.put("/user/*","anon");
        map.put("/product/*","perms[1]");//权限
        map.put("/admin","roles[1]");//角色
        map.put("/hot_event/add_event","roles[1]");
//        map.put("/hot_event/get_event","perms[0]");
        map.put("/hot_event/delete_event/{id}","roles[1]");
        map.put("/company_req_product/add_product","roles[1]");
//        map.put("/appointment/make_appointment","roles[0]");
//        设置过滤器
        filterFactoryBean.setFilterChainDefinitionMap(map);

        return filterFactoryBean;
    }
}
